package com.itzixi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itzixi.common.pojo.JqGridResult;
import com.itzixi.mapper.DataArticleMapper;
import com.itzixi.pojo.DataArticle;
import com.itzixi.service.DataArticleService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Service
public class DataArticleServiceImpl implements DataArticleService{


    @Autowired
    private DataArticleMapper dataArticleMapper;

    @Autowired
    private Sid sid;

    @Override
    public void saveDataArticle(DataArticle dataArticle) {
        String itemId = sid.nextShort();
        dataArticle.setId(itemId);
        dataArticleMapper.insert(dataArticle);
    }

    @Override
    public JqGridResult queryDataArticleList(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<DataArticle> list =  dataArticleMapper.selectAll();
        PageInfo<DataArticle> pageList = new PageInfo<DataArticle>(list);

        JqGridResult grid = new JqGridResult();
        grid.setTotal(pageList.getPages());
        grid.setRows(list);
        grid.setPage(pageList.getPageNum());
        grid.setRecords(pageList.getTotal());
        return grid;
    }

    @Override
    public DataArticle queryDataArticleById(String dataArticleId) {
        return dataArticleMapper.selectByPrimaryKey(dataArticleId);
    }

    @Override
    public void deleteDataArticleById(String dataArticleId) {
        dataArticleMapper.deleteByPrimaryKey(dataArticleId);
    }

    @Override
    public void updateDataArticleById(DataArticle dataArticle) {
        dataArticleMapper.updateByPrimaryKey(dataArticle);
    }
}
